package com.xhtt.modules.app.controller.reportform;

import com.xhtt.common.exception.RRException;
import com.xhtt.common.exception.RRExceptionEnum;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.DateUtils;
import com.xhtt.common.utils.ExportExcel;
import com.xhtt.common.utils.R;
import com.xhtt.modules.reportform.service.TttReportService;
import com.xhtt.modules.reportform.service.VitReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统计分析-TTT统计报表
 *
 * @Date 2019/3/15 14:32
 **/

@RestController
@RequestMapping(value = "app/report/ttt")
@Api(value = "统计分析-TTT统计报表", tags = "统计分析-TTT统计报表")
public class TTTReportController {

    @Autowired
    private TttReportService tttReportService;

    @Autowired
    private VitReportService vitReportService;

    @GetMapping(value = "/total")
    @ApiOperation("TTT时间总表")
    public R getTotal(@RequestParam Map<String, Object> params) {
        Integer type = Integer.parseInt(params.get("type").toString());
        List<Map<String, Object>> result = new ArrayList<>();
        if (type.equals(1)) {
            result = tttReportService.totalDay(params);
        } else if (type.equals(2)) {
            result = vitReportService.dataOfYear(params, 4);
        }
        return R.ok().put(Constant.DATA, result);
    }

    @GetMapping(value = "/total/export")
    @ApiOperation("TTT时间总表-导出")
    public void totalExport(HttpServletResponse response, @RequestParam Map<String, Object> params) {

        Integer type = Integer.parseInt(params.get("type").toString());
        List<Map<String, Object>> result = tttReportService.totalDay(params);
        ExportExcel exportExcel = new ExportExcel();
        params.put("limit", "650000");
        String fileName = "TTT时间总表";
        String sheetName = "日期";
        String[] headers = new String[]{"日期", "装车时间", "装车数量"};
        String[] columns = new String[]{"date", "show_time", "count"};
        if (type.equals(2)) {
            result = vitReportService.dataOfYear(params, 4);
            fileName = "TTT时间总表";
            sheetName = "年份";
            headers = new String[]{"月份", "每月TTT时间", "装车数量"};
            columns = new String[]{"month", "sum", "count"};
        }

        try {
            exportExcel.exportExcel(fileName, sheetName, headers, columns, result, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException(RRExceptionEnum.EXPORT_FAIL);
        }
    }

    @GetMapping(value = "/item")
    @ApiOperation("各时间段占比统计")
    public R getItem(@RequestParam Map<String, Object> params) {
        return R.ok().put(Constant.DATA, tttReportService.item(params));
    }

    @GetMapping(value = "/item/export")
    @ApiOperation("各时间段占比统计-导出")
    public void itemExport(HttpServletResponse response, @RequestParam Map<String, Object> params) {

        List<Map<String, Object>> result = tttReportService.item(params);
        Map<String, Object> cur = result.get(0);
        List<Map<String, Object>> r = new ArrayList<>();
        cur.forEach((k, v) -> {
            switch (k) {
                case "show_time1" : {
                    Map<String, Object> data = new HashMap<>();
                    data.put("show_time", v);
                    data.put("type", "开票-过磅");
                    r.add(data);
                    break;
                }
                case "show_time2" : {
                    Map<String, Object> data = new HashMap<>();
                    data.put("show_time", v);
                    data.put("type", "过磅-上台");
                    r.add(data);
                    break;
                }
                case "show_time3" : {
                    Map<String, Object> data = new HashMap<>();
                    data.put("show_time", v);
                    data.put("type", "上台-启泵");
                    r.add(data);
                    break;
                }
                case "show_time4" : {
                    Map<String, Object> data = new HashMap<>();
                    data.put("show_time", v);
                    data.put("type", "启泵-停泵");
                    r.add(data);
                    break;
                }
                case "show_time5" : {
                    Map<String, Object> data = new HashMap<>();
                    data.put("show_time", v);
                    data.put("type", "停泵-下台");
                    r.add(data);
                }
            }
        });
        ExportExcel exportExcel = new ExportExcel();
        params.put("limit", "650000");
        String fileName = "个时间段占比统计";
        String sheetName = "sheet1";
        String[] headers = new String[]{"类型", "时间"};
        String[] columns = new String[]{"type", "show_time"};

        try {
            exportExcel.exportExcel(fileName, sheetName, headers, columns, r, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException(RRExceptionEnum.EXPORT_FAIL);
        }
    }

    @GetMapping(value = "/car")
    @ApiOperation("每台发车数统计")
    public R getCar(@RequestParam Map<String, Object> params) {
        return R.ok().put(Constant.DATA, tttReportService.carDay(params));
    }

    @GetMapping(value = "/car/export")
    @ApiOperation("每台发车数统计-导出")
    public void carExport(HttpServletResponse response, @RequestParam Map<String, Object> params) {

        List<Map<String, Object>> result = tttReportService.carDay(params);
        ExportExcel exportExcel = new ExportExcel();
        params.put("limit", "650000");
        String fileName = "每台发车数统计";
        String sheetName = "日期";
        String[] headers = {"台号", "发车数"};
        String[] columns = {"name", "count"};
        if (params.get("type").toString().equals("2")) {
            fileName = "每台发车数统计";
            sheetName = "年份";
            headers = new String[]{"台号", "发车数"};
            columns = new String[]{"name", "count"};
        }

        try {
            exportExcel.exportExcel(fileName, sheetName, headers, columns, result, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException(RRExceptionEnum.EXPORT_FAIL);
        }
    }
}
